package com.mxx.common.conv;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mxx.common.utils.date.Pattern;

import java.util.List;
import java.util.Map;

/**
 * @Author itzgyw
 * @Date 2020-01-10
 * @Time 18:01
 * @Description:
 */
public class JsonUtil {

    public static String toJSONString(Object javaObject) {
        return JSONObject.toJSONString(javaObject);
    }

    public static String toJSONStringWithDateFormat(Object javaObject) {
        return JSONObject.toJSONStringWithDateFormat(javaObject, Pattern.DATE_TIME);
    }

    public static String toJSONStringWithDateFormat(Object javaObject, String dateFormat) {
        return JSONObject.toJSONStringWithDateFormat(javaObject, dateFormat);
    }

    public static JSONObject toJSON(Object javaObject) {
        if (javaObject instanceof String) {
            return JSONObject.parseObject((String) javaObject);
        }
        return (JSONObject) JSONObject.toJSON(javaObject);
    }

    public static <T> T toJavaObject(Map map, Class<T> classes) {
        return toJSON(map).toJavaObject(classes);
    }

    public static <T> T toJavaObject(String jsonText, Class<T> classes) {
        return JSONObject.parseObject(jsonText).toJavaObject(classes);
    }

    public static <T> List<T> toJavaArray(String jsonText, Class<T> classes) {
        return JSONObject.parseArray(jsonText).toJavaList(classes);
    }

    public static JSONArray toJSONArray(String jsonText) {
        return JSONObject.parseArray(jsonText);
    }
}
